package com.navinfo.data.esper.cep.marketdatafeed;

import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.UpdateListener;

public class TicksPatternStatement {
    public EPStatement getStatement() {
        return statement;
    }

    private EPStatement statement;

    public TicksPatternStatement(EPAdministrator admin) {

//        String pattern = "every (a=MarketDataEvent(feed.toString()='FEED_A')-> (timer:interval(10 sec) and b=MarketDataEvent(feed.toString()='FEED_B')))";
//        String pattern = "every ((([:1] (a0=MarketDataEvent(feed.toString()='FEED_A')) until (b0=MarketDataEvent(feed.toString()='FEED_A'))) -> ([:1] (a=MarketDataEvent(feed.toString()='FEED_A')) until (b=MarketDataEvent(feed.toString()='FEED_B'))) -> ([:1] (c=MarketDataEvent(feed.toString()='FEED_A')) until (d=MarketDataEvent(feed.toString()='FEED_B')))) where timer:within (10 seconds))";
        String pattern = "(([:1] (a0=MarketDataEvent(feed.toString()='FEED_A')) until (b0=MarketDataEvent(feed.toString()='FEED_A'))) -> ([:1] (a=MarketDataEvent(feed.toString()='FEED_A')) until (b=MarketDataEvent(feed.toString()='FEED_B'))) -> ([:1] (c=MarketDataEvent(feed.toString()='FEED_A')) until (d=MarketDataEvent(feed.toString()='FEED_B')))) and MarketDataEvent(symbol='SYM')";
        statement = admin.createPattern(pattern);
//        epServiceProvider.getEPAdministrator().destroyAllStatements();
//        epServiceProvider.removeAllStatementStateListeners();
        /*
        select * from TemperatureSensorEvent
            match_recognize (
            partition by device
            measures first(A.id) as first_a, last(A.id) as last_a, B[0].id as b0_id,
            B[1].id as b1_id
            pattern (A+ B+)
            define
            A as A.temp >= 100,
            B as B.temp > A.temp)
         */
    }

    public void addListener(UpdateListener listener) {
        statement.addListener(listener);
    }
}
